package com.le.tester.webpage.redis.controller;

import com.le.tester.common.ServerResponse;
import com.le.tester.webpage.redis.service.RedisRankingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 描述：redis实现排行榜功能，分数会实时的变化，不使用oracle数据库的row_num进行进行全表扫描的排序
 */
@Controller
@RequestMapping("/")
public class RedisRankingController {

    private static final Logger LOG = LoggerFactory.getLogger(RedisRankingController.class);


    @Autowired
    private RedisRankingService redisRankingService;

    @ResponseBody
    @RequestMapping("getTopTen")
    public ServerResponse<Map<String, Object>> getRankTopTen() {

        try {
            Map<String, Object> topTen = redisRankingService.getTopTen();
            return ServerResponse.createBySuccess(topTen);
        } catch (Exception e) {
            LOG.info("获取实时排行前十异常：{}", e.getMessage());
            return ServerResponse.createByError();
        }

    }
}
